#!/bin/sh
TIME_FORMAT="%Y-%m-%d %H:%M:%S"
# DINGTALK_ACCESSTOKEN="c99b6704751bc67ce0d02efd7bf219ef5899ede586b602325ae35fbf2d5ecccb"
PGYER_API_KEY="78c83aa2cc2846845ff30092040a3b3a"

BUILD_CHANEL_SHORTCUT="hl_todo"
DOWNLOAD_URL="https://www.pgyer.com/$BUILD_CHANEL_SHORTCUT"
QRCODE_URL="https://www.pgyer.com/app/qrcode/$BUILD_CHANEL_SHORTCUT"

echo $QRCODE_URL

# DINGTALK_URL="https://oapi.dingtalk.com/robot/send?access_token=$DINGTALK_ACCESSTOKEN"
START_TIME=`date +"$TIME_FORMAT"`
echo "开始打包时间：$START_TIME"
WORKDIR=`pwd`
echo "当前工作目录为：$WORKDIR"
echo "开始打包"
flutter build apk --target-platform android-arm,android-arm64
APKDIR=$WORKDIR/build/app/outputs/apk/release/app-release.apk
echo "打包完成,包路径为:$APKDIR"
echo "开始上传"
curl -F "file=@$APKDIR" -F "_api_key=$PGYER_API_KEY" https://www.pgyer.com/apiv2/app/upload
echo "上传完成"

# END_TIME=`date +"$TIME_FORMAT"`
# START_TIMESTAMP=$(date -j -f "$TIME_FORMAT" "$START_TIME" +%s);
# END_TIMESTAMP=$(date -j -f "$TIME_FORMAT" "$END_TIME" +%s);
# TASK_TIME=$((END_TIMESTAMP-START_TIMESTAMP));
# CONTENT="#### CheChen打包服务 \n> 【打包环境】正式环境 \n\n> 【Platform】Android \n\n> 【开始时间】${START_TIME} \n\n> 【结束时间】${END_TIME} \n\n> 【任务耗时】${TASK_TIME}s \n\n> 【下载二维码】 \n\n> ![screenshot]($QRCODE_URL) \n\n> 【下载地址】[点击查看]($DOWNLOAD_URL) \n\n"

echo "结束打包时间：$END_TIME"
# echo "开始通知"

# curl $DINGTALK_URL \
# -H 'Content-Type: application/json' \
# -d "{\"msgtype\": \"markdown\",\"markdown\": {\"title\":\"CheChen打包服务\",\"text\": \"${CONTENT}\"},\"at\": {\"atMobiles\": [\"13648091632\",], \"isAtAll\": true}}"
